package jlxy.ise.eroad.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.google.gson.Gson;
import com.loopj.android.http.JsonHttpResponseHandler;

import org.json.JSONObject;

import cz.msebera.android.httpclient.Header;
import jlxy.ise.eroad.R;
import jlxy.ise.eroad.base.BaseSwitchFragment;
import jlxy.ise.eroad.bean.ServerRpsInfo;
import jlxy.ise.eroad.util.SingleToast;
import jlxy.ise.eroad.util.WebUtil;


/**
 * 用户信息页面
 *
 * @author chen
 */
public class PersonFragment extends BaseSwitchFragment implements OnClickListener {

    private static final String TAG = PersonFragment.class.getSimpleName();

    private View rootView;
    private EditText mEtName;
    private EditText mEtDriverNum;
    private EditText mEtEmePerson;
    private EditText mEtEmeTel;

    private static PersonFragment instance;

    public PersonFragment() {
    }

    public static PersonFragment getInstance() {
        if (instance == null) instance = new PersonFragment();
        return instance;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_personinfo, container,
                false);
        mEtName = (EditText) rootView.findViewById(R.id.et_username);
        mEtDriverNum = (EditText) rootView.findViewById(R.id.et_drivingnumber);
        mEtEmePerson = (EditText) rootView.findViewById(R.id.et_em_person);
        mEtEmeTel = (EditText) rootView.findViewById(R.id.et_em_person_tel);
        Button mBtn = (Button) rootView.findViewById(R.id.btn_modify);
        mBtn.setOnClickListener(this);

        mEtName.setText(mApp.getDriverInfo().getName());
        mEtDriverNum.setText(mApp.getDriverInfo().getDrivingnumber());
        mEtEmePerson.setText(mApp.getDriverInfo().getEmergencyperson());
        mEtEmeTel.setText(mApp.getDriverInfo().getEmergencyperson());
        return rootView;
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_modify:
                // 提交修改
                doModifyUserInfo();
                break;
            default:
                break;
        }
    }

    private void doModifyUserInfo(){

        final String name = mEtName.getEditableText().toString();
        final String drivingnumber = mEtDriverNum.getEditableText().toString();
        final String emergencyperson = mEtEmePerson.getEditableText().toString();
        final String emergencynumber = mEtEmeTel.getEditableText().toString();

        WebUtil.postAddDriverUser2(WebUtil.getRpAddDriverUser2(mApp.getDriverInfo().getId() + "",
                name,drivingnumber,emergencyperson,emergencynumber), new JsonHttpResponseHandler() {

            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                Gson gson = new Gson();
                try {
                    ServerRpsInfo serverRpsInfo = gson.fromJson(response.toString(), ServerRpsInfo.class);
                    if (serverRpsInfo.getStatus().equals("ok") && serverRpsInfo.getMessage().equals("ok")) {
                        SingleToast.showToast(mAct, "修改信息成功", 2000);
                        mApp.getDriverInfo().setName(name);
                        mApp.getDriverInfo().setDrivingnumber(drivingnumber);
                        mApp.getDriverInfo().setEmergencynumber(emergencynumber);
                        mApp.getDriverInfo().setEmergencyperson(emergencyperson);
                        MyFragment.isRefresh = true;
                    }
                } catch (Exception e) {
                    SingleToast.showToast(mAct, "修改信息出错:" + e.getLocalizedMessage(), 2000);
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
                super.onFailure(statusCode, headers, throwable, errorResponse);
                SingleToast.showToast(mAct, "修改信息失败", 2000);
            }
        });

    }




}
